import 'package:dss_base/dss_base.dart';
import 'package:dss_base/res/colors_x.dart';
import 'package:dss_base/utils/font_weight_util.dart';
import 'package:dss_base/widgets/basis/container_x.dart';
import 'package:dss_base/widgets/basis/image_x.dart';
import 'package:dss_base/widgets/image_back.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class AppBarX extends StatelessWidget implements PreferredSizeWidget {
  const AppBarX({
    super.key,
    required this.title,
    // 标题字体样式
    this.titleColor = ColorsX.mainText,
    this.titleFontSize = 18,
    this.titleFontWeight = FontWeightUtil.pingFangSCSemibold,
    this.titleTextAlign = TextAlign.start,
    this.isShowBack = true,
    this.isBlackBack = true,
    // 标题及状态栏颜色
    this.backgroundColor = Colors.white,
    // 状态栏字体颜色是否为黑色
    this.isBlackStatusFontColor = true,
    this.rightWidget,
    this.onBackPressed,
  });

  final String title;
  final Color titleColor;
  final double titleFontSize;
  final FontWeight titleFontWeight;
  final TextAlign titleTextAlign;

  final Color backgroundColor;

  /// 是否显示返回按钮
  final bool isShowBack;

  /// 点击返回按钮的点击事件
  final GestureTapCallback? onBackPressed;

  /// 返回按钮是否为黑色的
  final bool isBlackBack;

  /// 系统状态栏颜色是否为黑色
  final bool isBlackStatusFontColor;

  final Widget? rightWidget;

  @override
  Widget build(BuildContext context) {
    // 标题
    Widget child = Text(
      title,
      textAlign: titleTextAlign,
      style: TextStyle(
        color: titleColor,
        fontSize: titleFontSize,
        fontWeight: titleFontWeight,
      ),
      maxLines: 1,
      overflow: TextOverflow.ellipsis,
    );
    if (titleTextAlign == TextAlign.center) {
      child = Stack(
        children: [
          if (isShowBack)
            Positioned(
              top: 0,
              bottom: 0,
              left: 0,
              child: ImageBack(isBlack: isBlackBack, onPressed: onBackPressed),
            ),
          Center(
            child: ConstrainedBox(
              constraints: BoxConstraints(
                maxWidth: MediaQuery.of(context).size.width - 200,
              ),
              child: child,
            ),
          ),
          if (rightWidget != null)
            Positioned(top: 0, bottom: 0, right: 0, child: rightWidget!),
        ],
      );
    } else {
      child = Row(
        children: [
          if (isShowBack)
            ImageBack(isBlack: isBlackBack, onPressed: onBackPressed)
          else
            Gaps.hGap10,
          Expanded(child: child),
          if (rightWidget != null) rightWidget!,
        ],
      );
    }

    // 整体
    child = Container(
      alignment: Alignment.center,
      width: double.infinity,
      child: child,
    );

    // 搞个状态栏变色神马的
    return AnnotatedRegion<SystemUiOverlayStyle>(
      // 状态栏字体颜色
      value: isBlackStatusFontColor
          ? SystemUiOverlayStyle.dark
          : SystemUiOverlayStyle.light,
      child: Material(
        // 状态栏背景色
        color: backgroundColor,
        child: SafeArea(child: child),
      ),
    );
  }

  @override
  Size get preferredSize => Size.fromHeight(48.h);
}

/// AppBar 顶部的按钮
class AppBarMenu extends StatelessWidget {
  final String icon;
  final double? width;
  final double? height;
  final double? padding;
  final GestureTapCallback onPressed;

  const AppBarMenu({
    super.key,
    required this.icon,
    required this.onPressed,
    this.width,
    this.height,
    this.padding,
  });

  @override
  Widget build(BuildContext context) {
    return ContainerX(
      onPressed: onPressed,
      width: width ?? 40.w,
      height: height ?? 40.w,
      padding: EdgeInsets.all(padding ?? 0),
      child: ImageX(urlOrPath: icon),
    );
  }
}
